﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using BryhtControls;
using BryhtCommon;
using System.IO;
using System.Windows.Media.Imaging;
using TencentWeiboSDK.Services;
using TencentWeiboSDK.Services.Util;
using System.Threading;

namespace MyRun.Views
{
    public partial class QQUploadPhoto : PhoneApplicationPage
    {
        public QQUploadPhoto()
        {
            InitializeComponent();
            GlobalIndicator.Initialize(this);
        }

        private string _picPath = string.Empty;
        private TService service = new TService();
        private string captionText = string.Empty;
        private BitmapImage imageSource;

        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            txtTitle.Text = "上传图片";
            if (NavigationContext.QueryString.ContainsKey("state"))
            {
                tbx_caption.Text = Utility.UriDecode(NavigationContext.QueryString["state"]);
            }
            //获取图片
            _picPath = "uploadpic.jpg";
            FileStream imageStream = null;
            if (Isolated.ReadFile(_picPath, out imageStream))
            {
                imageSource = new BitmapImage();
                imageSource.SetSource(imageStream);
                img_pic.Source = imageSource;
            }
            imageStream.Dispose();
        }

        private void tbx_caption_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (tbx_caption.Text.Length > 140)
            {
                tbx_caption.Text = tbx_caption.Text.Substring(0, 140);

                tbx_caption.SelectionStart = 140;
                MessageBoxResult result = MessageBox.Show("描述文字不能超过140字！");
            }
            captionText = tbx_caption.Text;
        }


        //上传
        private void upload_Click(object sender, RoutedEventArgs e)
        {
            GlobalIndicator.Instance.IsBusy = true;
            GlobalIndicator.Instance.Text = "上传中...";

            UploadPic pic = (null != imageSource) ? new UploadPic(imageSource) : null;

            new Thread(() =>
            {
                Action<Callback<object>> action1 = new Action<Callback<object>>((callback) =>
                {

                    if (null == callback.InnerException)
                    {
                        GlobalIndicator.Instance.IsBusy = false;
                        ThrowMessage.Show(MessageType.Prompt, "上传成功！");
                    }
                    else
                    {
                        ThrowMessage.Show(MessageType.Error, callback.InnerException.Message);
                    }

                    imageSource = null;

                });

                if (null != imageSource)
                {
                    service.AddPic(new ServiceArgument() { Content = captionText, Pic = pic }, action1);
                }

            }).Start();

        }

        //取消
        private void cancel_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GoBack();
        }
    }
}